package it.heima.xmy.demo;

import java.io.*;

/*在D盘中创建两个文件夹，img1,img2。
在img1中放置一个图片文件，将img1中的图片文件使用字节流复制到img2中。*/
public class ByteStreamCopyPicture {
    public static void main(String[] args) {
        File file = new File("d:\\img1\\123.jpg");
        File file1 = new File("d:\\img2\\123.jpg");
        copyPicture(file, file1);
    }

    public static void copyPicture(File file, File file1) {
        //使用字节流进行复制
        //创建字节流对象
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(file);
            os = new FileOutputStream(file1);
            //使用字节流对象读写字节数组进行复制
            byte[] bytes = new byte[(int) Math.pow(1024, 2)];
            int len = -1;
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
                System.out.println("拷贝完成");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
